package com.yanqu.road.server.cross3server.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.logic.activity.entername.ChannelEnterNameHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.cross3server.manager.Cross3EnterNameMgr;
import com.yanqu.road.server.protocol.Cross3Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = Cross3Protocol.C3_ENTER_NAME_BATCH_ADD_FROM_CHANNEL, desc = "自动报名返回(批量)")
public class Cross3EnterNameBatchAddFromChannelCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        EnterNameProto.ChannelEnterNameAllSyncMsg syncMsg = EnterNameProto.ChannelEnterNameAllSyncMsg.parseFrom(yanQuMessage.getBytes());
        int activityId = syncMsg.getActivityId();
        Map<Long, UserEnterName> map = new ConcurrentHashMap<>();
        for (EnterNameProto.UserEnterNameMsg msg : syncMsg.getNameList()) {
            map.put(msg.getUserId(), ChannelEnterNameHelper.parseFromMsg(msg));
        }

        Cross3EnterNameMgr.add(activityId, map);
        Cross3EnterNameMgr.getLogger().info("enter name cache activityId {}, user size {}", activityId, map.size());
    }
}
